﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Siemens.Engineering;
using Siemens.Engineering.HW;
using Siemens.Engineering.HW.Features;
using Siemens.Engineering.SW;
using Siemens.Engineering.SW.Blocks;
using Siemens.Engineering.SW.ExternalSources;
using Siemens.Engineering.SW.Tags;
using Siemens.Engineering.SW.Types;
using Siemens.Engineering.Hmi;
using HmiTarget = Siemens.Engineering.Hmi.HmiTarget;
using Siemens.Engineering.Hmi.Tag;
using Siemens.Engineering.Hmi.Screen;
using Siemens.Engineering.Hmi.Cycle;
using Siemens.Engineering.Hmi.Communication;
using Siemens.Engineering.Hmi.Globalization;
using Siemens.Engineering.Hmi.TextGraphicList;
using Siemens.Engineering.Hmi.RuntimeScripting;
using Siemens.Engineering.Compiler;
using Siemens.Engineering.Library;
using System.IO;

namespace TIA_Openness
{
    public static class HMI_TagTable_Helper
    {
        /// <summary>
        /// 从指定文件夹导出所有HMI变量表
        /// </summary>
        /// <param name="hmitarget"></param>
        /// <param name="path">路径</param>
        public static void ExportAllTagTablesFromTagFolder(HmiTarget hmitarget, string path )
        {
            TagSystemFolder folder = hmitarget.TagFolder;
            TagTableComposition tables = folder.TagTables;
            foreach (TagTable table in tables)
            {
                FileInfo info = new FileInfo(string.Format(path + @"\" + table.Name + ".xml"));
                table.Export(info, ExportOptions.WithDefaults);
            }
        }

        /// <summary>
        /// 导出单个HMI变量表
        /// </summary>
        /// <param name="hmitarget"></param>
        /// <param name="tableName">举例 Tag table XYZ</param>
        /// <param name="path">路径</param>
        public static void ExportTagTableFromHMITarget(HmiTarget hmitarget, string tableName, string path )
        {
            
            TagSystemFolder folder = hmitarget.TagFolder;
            TagTableComposition tables = folder.TagTables;
            TagTable table = tables.Find(tableName);

            if (table != null)
            {
                FileInfo info = new FileInfo(string.Format(path + @"\" + table.Name + ".xml"));
                table.Export(info, ExportOptions.WithDefaults);
            }
        }

        /// <summary>
        /// 导出所有变量表
        /// </summary>
        /// <param name="hmitarget"></param>
        /// <param name="path">路径</param>
        public static void ExportAllTagTablesFromHMITarget(HmiTarget hmitarget, string path)
        {
            TagSystemFolder sysFolder = hmitarget.TagFolder;

            //First export the tables in underlying user folder
            foreach (TagUserFolder userFolder in sysFolder.Folders)
            {
                ExportUserFolderDeep(userFolder, path);
            }

            //then, export all tables in the system folder
            ExportTablesInSystemFolder(sysFolder, path);
        }

        private static void ExportUserFolderDeep(TagUserFolder rootUserFolder, string path)
        {
            foreach (TagUserFolder userFolder in rootUserFolder.Folders)
            {
                ExportUserFolderDeep(userFolder,  path);
            }
            ExportTablesInUserFolder(rootUserFolder,  path);
        }
        private static void ExportTablesInUserFolder(TagUserFolder folderToExport, string path)
        {
            TagTableComposition tables = folderToExport.TagTables;
            foreach (TagTable table in tables)
            {
                string fullFilePath = string.Format(path + @"\" + table.Name + ".xml");
                table.Export(new FileInfo(fullFilePath), ExportOptions.WithDefaults);
            }
        }
        private static void ExportTablesInSystemFolder(TagSystemFolder folderToExport, string path)
        {
            TagTableComposition tables = folderToExport.TagTables;
            foreach (TagTable table in tables)
            {
                string fullFilePath = string.Format(path + @"\" + table.Name + ".xml");
                table.Export(new FileInfo(fullFilePath), ExportOptions.WithDefaults);
            }
        }
    }
}
